<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
	/* @var $repository escape_repository_Repository */
	$repository =& $this->requestAttributes['repository'];
	/* @var $repositoryUi escape_repositoryui_RepositoryUi */
	$repositoryUi =& $this->requestAttributes['repositoryUi'];
	/* @var $errorMessages array */
	$errorMessages =& $this->requestAttributes['errorMessages'];

	$gadgetModeUrlParameter = '';
	if($repositoryUi->isGadgetMode())
	{
		$gadgetModeUrlParameter = '?gadget=1';
	}
	
	$fullname = $this->requestAttributes['fullname'];
	$email = $this->requestAttributes['email'];
	$organization = $this->requestAttributes['organization'];
	$dai = $this->requestAttributes['dai'];
	$password = $this->requestAttributes['password'];
		
	$user = $repositoryUi->getUser();
	
	$userDataSource = $user->getDataSource();
	$userIsProvisioned = false;
	if($userDataSource !== null && $userDataSource !== '')
	{
		$userIsProvisioned = true;
	}
	
	$openIds = $user->getOpenIds();
	
	$templateVariables = array();
	$templateVariables['title'] = 'Account details';
	$this->requestAttributes['templateVariables'] =& $templateVariables;
?>
<?php $this->displayHeader(); ?>

<div class="escape_Section">

<div class="escape_Selection">

<div class="escape_SectionTitle">Edit ESCAPE account details</div>

<?php
	if(count($errorMessages) > 0)
	{
?>
<div>
<?php
		foreach($errorMessages as $errorMessage)
		{
?>
<div class="escape_Error"><?php print $errorMessage; ?></div>
<?php
		}
?>
</div>
<br />
<?php
	}
?>

<form method="post" action="/account<?php print $gadgetModeUrlParameter; ?>" autocomplete="off">
<input type="hidden" name="verb" value="updateAccount" />

<table class="escape_FormTable">
<tr>
<td>
User ID:
</td>
<td>
<?php print htmlspecialchars($user->getId()); ?>
</td>
</tr>
<tr>
<td>
Full name:
</td>
<td>
<input type="text" name="fullname" value="<?php print htmlspecialchars($fullname); ?>" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="email" value="<?php print htmlspecialchars($email); ?>" />
</td>
</tr>
<tr>
<td>
Organization:
</td>
<td>
<input type="text" name="organization" value="<?php print htmlspecialchars($organization); ?>" /> <span class="escape_Note">(optional)</span>
</td>
</tr>
<tr>
<td>
DAI <?php $repositoryUi->displayInfoTooltip('Digital Author Identifier'); ?>:
</td>
<td>
<input type="text" name="dai" value="<?php print htmlspecialchars($dai); ?>" /> <span class="escape_Note">(optional)</span>
</td>
</tr>
<?php
	// don't allow password changes for provisioned users
	if(!$userIsProvisioned)
	{
?>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="password" value="" /> <span class="escape_Note">(leave this field blank to keep your current password)</span>
</td>
</tr>
<?php
	}
?>
</table>

<?php
	if($userIsProvisioned)
	{
?>
<p class="escape_Note"><em>Warning: this user account was created automatically, your account details might be overwritten next time you log on.</em></p>
<?php
	}
?>

<input type="submit" value="Update account" />
<button onclick="escape_escapeUi.redirectToResourceById('me'); return false">Cancel</button>

</form>

</div>

<?php
	// only show the OpenID options if OpenID authentication is enabled and the user object isn't provisioned
	if($repository->config['localauthn_enabled'] && $repository->config['signup_enabled'] && $repository->config['openidauthn_enabled'] && !$userIsProvisioned)
	{
?>
<div class="escape_SectionTitle">
OpenIDs:
</div>

<?php
		if(count($openIds) === 0)
		{
?>
<p>
This account is not connected to any OpenID.
</p>
<?php
		}
		else
		{
?>
<p>
The following OpenIDs are connected to this account:
</p>
<ul>
<?php
			foreach($openIds as $openId)
			{
				$shortIdentifier = escape_getOpenIdShortIdentifier($openId);
?>
<li>
<?php print htmlspecialchars($shortIdentifier); ?> <span class="escape_Actions">[&nbsp;<a href="/account/?<?php print substr($gadgetModeUrlParameter, 1) . '&amp;'; ?>verb=removeOpenId&amp;openId=<?php print rawurlencode($openId)?>">Disconnect</a>&nbsp;]</span>
</li>
<?php
			}
?>
</ul>
<?php
		}
?>

<p>
Add an OpenID to this account:
</p>

<?php
		$repositoryUi->displayOpenIdLoginForm('AddOpenId', 'Account');
?>

</div>
<?php
	}
?>

<?php $this->displayFooter(); ?>
